export PROJECT_JUSTFILE := "1" # Note: used in build.py
PG_MAJOR := env("PG_MAJOR", "17")
PG_BIN := env("PG_BIN", "/usr/lib/postgresql/" + PG_MAJOR + "/bin")
OVERWRITE_GOLDEN := env("OVERWRITE_GOLDEN", "false")

# Show list of recipes
default:
    @just --list

ci: docker-build docker-run docker-sync
  #!/usr/bin/env bash
  set -euo pipefail
  pwd
  trap "python3 build.py docker-stop; python3 build.py docker-rm" EXIT
  docker exec pgai-db just build
  docker exec pgai-db just lint
  docker exec -d pgai-db just test-server
  docker exec -e OVERWRITE_GOLDEN={{OVERWRITE_GOLDEN}} pgai-db just test

clean:
	@./build.py clean

build:
	@PG_BIN={{PG_BIN}} ./build.py build

build-check:
	@PG_BIN={{PG_BIN}} ./build.py build check

test-server:
	@./build.py test-server

test:
	@./build.py test

lint:
	@./build.py lint

docker-build:
	@PG_MAJOR={{PG_MAJOR}} ./build.py docker-build

docker-run:
	@./build.py docker-run

docker-start:
	@./build.py docker-start

docker-stop:
	@./build.py docker-stop

docker-rm:
	@./build.py docker-rm

docker-sync:
	@./build.py docker-sync

freeze:
	@./build.py freeze

# Launches a bash shell in the container
docker-shell:
	@docker exec -it -u root pgai-db /bin/bash

# Installs the db in the given target
@install target_db: build
    psql -d {{target_db}} -c 'truncate ai.pgai_lib_version' || true
    echo 'Installing pgai db...'
    uv run pgai install --strict -d {{target_db}}

# Installs the db in the given target with a clean installation
[confirm("This command will drop the `ai` schema and run a clean installation. Are you sure you want to proceed?")]
@install-clean target_db:
    echo 'Removing any previous installation...'
    psql -d {{target_db}} -c 'drop schema ai cascade'
    just install target_db

# Launches a psql shell in the container
psql-shell:
	@docker exec -it -u postgres pgai-db /bin/bash -c "set -e; if [ -f .env ]; then set -a; source .env; set +a; fi; psql"